CKEDITOR.plugins.add('pageSizeDelimiter',{
  
  init: function(editor){
    var pluginName = 'pageSizeDelimiter';
    
    editor.pageSize = new Object();
    editor.pageSize.pageMargin = 10;
    editor.pageSize.pageWidth = 190;
    editor.pageSize.pageHeight = 277;

    editor.addCss('html { background-color: #333333 !important;}' +
      ' body { position: relative; min-height: ' + editor.pageSize.pageHeight + 'mm !important; width: ' + editor.pageSize.pageWidth + 'mm !important; background-color: white !important; margin: 25px auto 25px; padding: ' + editor.pageSize.pageMargin + 'mm;}');
      var pages = 1;
      
      editor.on('saveSnapshot', function (e){
        var body = editor.document.$.body;
        var height = 0;
        
        for(i = 0; i < body.childNodes.length; i++){
          height = height + body.childNodes[i].scrollHeight;
          if(height > (pages * editor.pageSize.pageHeight)){
            //var elem = document.createElement('<div class="divPageBreaker" style="position: absolute; top: ' + pages * editor.pageSize.pageHeight + 'px; width:'+ editor.pageSize.pageWidth +'px; height:1px; background-color:blue;"></div>');
            var elem = document.createElement('div');
            elem.setAttribute('class', 'divPageBreaker');
            elem.setAttribute('style', 'position: absolute; top: ' + pages * editor.pageSize.pageHeight + 'px; width:'+ editor.pageSize.pageWidth +'px; height:1px; background-color:blue;');
            pages = pages + 1;
            body.appendChild(elem);
          }
        }
        
        
      });
      
      CKEDITOR.on( 'instanceReady', function( e ){       
        editor.pageSize.pageMargin = 10 * (editor.document.$.body.scrollWidth / 210);
        editor.pageSize.pageWidth = editor.document.$.body.scrollWidth;
        editor.pageSize.pageHeight = editor.document.$.body.scrollHeight;  
        
        editor.pageSize.pageWidth = editor.pageSize.pageWidth - editor.pageSize.pageMargin - editor.pageSize.pageMargin * 2;
        editor.pageSize.pageHeight = editor.pageSize.pageHeight;
      });
        
  
  }
});